package com.ws.webpacket.activity.dialog;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.ws.webpacket.BuildConfig;
import com.ws.webpacket.R;
import com.ws.webpacket.internet.been.B_Ping;


import java.util.List;

public class UrlSelectDialog extends DialogFragment {

    private List<B_Ping> pings;
    private RecyclerView recyclerView;
    private UrlAdapter urlAdapter;
    
    private UrlSelectAction urlSelectAction;

    private Button b_ReturnHome;
    private Button b_Refresh;
    private Button b_FloatButton;
    private Button b_FloatButton1;
    private Button b_PlayerService;

    public void onResume() {
        super.onResume();
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        getDialog().getWindow().setLayout((int) (dm.widthPixels * 1), (int) (dm.heightPixels * 0.45));
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_urlselect, container);
        urlAdapter = new UrlAdapter();
        recyclerView = view.findViewById(R.id.recyclerView_SelectDialog);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(urlAdapter);

        b_ReturnHome = view.findViewById(R.id.button_UrlSelectDialog_ReturnHome);
        b_ReturnHome.setOnClickListener(onClickListener);
        b_Refresh = view.findViewById(R.id.button_UrlSelectDialog_Refresh);
        b_Refresh.setOnClickListener(onClickListener);
        b_FloatButton = view.findViewById(R.id.button_UrlSelectDialog_FloatWindow);
        b_FloatButton1 = view.findViewById(R.id.button_UrlSelectDialog_Clear_cache);
        b_FloatButton.setOnClickListener(onClickListener);
        b_FloatButton1.setOnClickListener(onClickListener);

        b_PlayerService = view.findViewById(R.id.button_MyPlayer);
        b_PlayerService.setOnClickListener(onClickListener);

        if (BuildConfig.APPLICATION_ID.equals("com.a2.ws.App")) {
            b_PlayerService.setVisibility(View.VISIBLE);
        }

        return view;
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.button_UrlSelectDialog_ReturnHome:
                    urlSelectAction.loadHome();
                    dismiss();
                    break;
                case R.id.button_UrlSelectDialog_Refresh:
                    urlSelectAction.rePingUrl();

                    break;
                case R.id.button_UrlSelectDialog_FloatWindow:
                    urlSelectAction.floatWindow();
                    break;

                case R.id.button_UrlSelectDialog_Clear_cache:
                    //跳转到已安装应用
                    Intent intent1 = new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                    startActivity(intent1);
                    break;

                case R.id.button_MyPlayer:
                    try {
                        Intent intent = new Intent(getContext(), Class.forName("tv.danmaku.ijk.app.activity.LiveServiceActivity"));
                        startActivity(intent);
                        dismiss();
                    } catch (ClassNotFoundException e) {
                        Toast.makeText(getContext(), "没有找到界面", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    private View.OnClickListener itemClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = (int) v.getTag();
            if (urlSelectAction != null) {
                urlSelectAction.selected(pings.get(position));
                urlAdapter.notifyDataSetChanged();
            }
        }
    };

    public void refreshUrlList(List<B_Ping> pings) {
        this.pings = pings;
        if (urlAdapter != null)
            urlAdapter.notifyDataSetChanged();
    }

    public void setUrlSelectAction(UrlSelectAction urlSelectAction) {
        this.urlSelectAction = urlSelectAction;
    }


    //====================================================================================
    // 适配器
    //====================================================================================

    private class UrlAdapter extends RecyclerView.Adapter<UrlAdapter.UrlHandler> {

        @NonNull
        @Override
        public UrlHandler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            parent = (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.item_url, null);
            return new UrlHandler(parent);
        }

        @Override
        public void onBindViewHolder(@NonNull UrlHandler holder, int position) {
            if(pings.size() <= 0){
                return ;
            }

            B_Ping ping = pings.get(position);
            holder.itemView.setTag(position);

            if (ping.isLoade()) {
                holder.showStatus.setVisibility(View.VISIBLE);
                holder.showInfo.setText("线路" + (position + 1) + "******  " + ping.getOverTime() / 10 + "s  (当前线路)");
            } else {
                holder.showStatus.setVisibility(View.INVISIBLE);
                holder.showInfo.setText("线路" + (position + 1) + ":******  " + ping.getOverTime() / 10 + "s");
            }
        }

        @Override
        public int getItemCount() {
            if (pings == null)
                return 0;
            return pings.size();
        }

        public class UrlHandler extends RecyclerView.ViewHolder {
            private TextView showInfo;
            private ImageView showStatus;

            public UrlHandler(@NonNull View itemView) {
                super(itemView);

                itemView.setOnClickListener(itemClickListener);
                showInfo = itemView.findViewById(R.id.poptext);
                showStatus = itemView.findViewById(R.id.itemimg);
            }
        }
    }

    //====================================================================================
    // 接口
    //====================================================================================
    public interface UrlSelectAction {

        public void selected(B_Ping url);

        public void rePingUrl();

        public void loadHome();

        public void floatWindow();
    }


    public interface ItemClickListener {
        public void item(int position);
    }

}
